using B.S.Data.MES.API.Applications.CangKu.Command;
using B.S.Domain;
using B.S.Infratrctrue;
using B.S.Requst;
using MediatR;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace B.S.Data.MES.API.Applications.CangKu.CommandHandlers
{
    /// <summary>
    /// 获取所有Tel表数据的命令处理器
    /// </summary>
    public class GetAllTelCommandHandler : IRequestHandler<GetAllTelCommand, APIRequst<List<Tel>>>
    {
        private readonly IBaseRepository<Tel> _telRepository;

        /// <summary>
        /// 构造函数，注入Tel表仓储
        /// </summary>
        /// <param name="telRepository">Tel表仓储</param>
        public GetAllTelCommandHandler(IBaseRepository<Tel> telRepository)
        {
            _telRepository = telRepository;
        }

        /// <summary>
        /// 处理获取所有Tel表数据的请求
        /// </summary>
        /// <param name="request">查询命令</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>包含Tel表数据列表的API响应</returns>
        public async Task<APIRequst<List<Tel>>> Handle(GetAllTelCommand request, CancellationToken cancellationToken)
        {
            // 创建API响应对象
            APIRequst<List<Tel>> response = new APIRequst<List<Tel>>();
            
            // 设置响应状态为成功
            response.Code = RequstCode.成功;
            response.Msg = RequstCode.成功.ToString();
            
            // 查询所有Tel表数据，按照时间倒序排列，最新的数据显示在前面
            var telData = _telRepository.GetAll()
                .ToList();
            
            // 设置响应数据
            response.Data = telData;
            
            // 返回响应结果
            return await Task.FromResult(response);
        }
    }
}